package com.example.fw;


import java.util.List;


import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends HelperBase{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null){
			rebuidCache();
		}
		return cachedContacts;
	}
	
		private void rebuidCache() {
			cachedContacts = new SortedListOf<ContactData>();
			manager.navigateTo().mainPage();
			List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
			
			int index = 1;
			
			for (WebElement checkbox : checkboxes) {
				
			index = index+1;
			click(By.xpath(".//*[@id='maintable']/tbody/tr[" +index+ "]/td[7]/a/img"));
			
			WebElement indFirstName = driver.findElement(By.name("firstname"));
			String fistName = indFirstName.getAttribute("value");
			
			WebElement indLastName = driver.findElement(By.name("lastname"));
			String lastName = indLastName.getAttribute("value");
			
			cachedContacts.add(new ContactData()
			.withFistName(fistName)
			.withLastName(lastName));
			
			click(By.linkText("home"));	
			}
	}

	
	
	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		rebuidCache();
		return this;
	}
	
	public ContactHelper modifyGroup(int index, ContactData contact,
			boolean MODIFICATION) {
		manager.navigateTo().mainPage();
		initContactModification(index+1);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		rebuidCache();
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		initContactModification(index+1);
		deleteContactModification();
		returnToHomePage();
		rebuidCache();
		return this;
	}
	
	//----------------------------------------------
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type (By.name("firstname"), contact.getFistName());
		type (By.name("lastname"), contact.getLastName());
		type (By.name("address"), contact.getFirstAddress());
		type (By.name("home"), contact.getTelHome());
		type (By.name("mobile"), contact.getTelMobile());
		type (By.name("work"), contact.getTelWork());
		type (By.name("email"), contact.getFistEmail());
		type (By.name("email2"), contact.getSecondEmail());
		selectByText(By.name("bday"), driver.findElement(By.xpath("//select[1]/option[" +contact.getIndexBirthDay()+ "]")).getText());
		selectByText(By.name("bmonth"), driver.findElement(By.xpath("//select[2]/option[" +contact.getIndexBirthMonth()+ "]")).getText());	
		type (By.name("byear"),"" +contact.getIndexBirthYear()+ "");
		
		if (formType == CREATION){
		   selectByText(By.name("new_group"), driver.findElement(By.xpath("//select[3]/option[" +contact.getIndexGroup()+ "]")).getText());
		} else {
 			if (driver.findElements(By.name("new_group")).size() !=0){
 				throw new Error("Group selector exists in contact modification form");
 			}
		}
		
		type (By.name("address2"), contact.getSecondAddress());
		type (By.name("phone2"), contact.getSecondTelHome());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}
	
	public ContactHelper returnToHomePage() {
		click(By.linkText("home"));
		return this;
	}

	
	public ContactHelper initContactModification(int index) {
		index = index+1;
		click(By.xpath(".//*[@id='maintable']/tbody/tr[" +index+ "]/td[7]/a/img"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper deleteContactModification() {
		click(By.xpath("/html/body/div/div[4]/form[2]/input[2]"));
		cachedContacts = null;
		return this;
	}
	
}
